package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * AxdrOptional类
 * 实现可选类型的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class AxdrOptional<T extends AxdrType> implements AxdrType {
    private T value = null;
    private boolean isUsed = false;
    private final Class<T> clazz;

    public AxdrOptional(Class<T> clazz) {
        this.clazz = clazz;
    }

    public AxdrOptional() {
        this.clazz = null;
    }

    public void setValue(T value) {
        this.value = value;
        this.isUsed = true;
    }

    public T getValue() {
        return this.value;
    }

    public boolean isUsed() {
        return this.isUsed;
    }

    public void setUsed(boolean used) {
        this.isUsed = used;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        // Implementation depends on specific use case
        return 0;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        // Implementation depends on specific use case
        return 0;
    }

    @Override
    public String toString() {
        if (this.isUsed && this.value != null) {
            return "optional: {" + this.value.toString() + "}";
        } else {
            return "optional: {not used}";
        }
    }
}